﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using iShoppingBUS;
using iShoppingDTO;

namespace iShopping.Manager.UserControl
{
    public partial class CapNhapThongTinSanPham : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                FillLoaiSanPham();
            }
        }

        private void FillLoaiSanPham()
        {
            DataTable dt = LoaiSanPhamBUS.LayDSLoaiSanPhamO();
            GRLoaiSanPham.DataSource = dt;
            GRLoaiSanPham.DataBind();
        }

        protected void GRLoaiSanPham_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            // phan trang
            GRLoaiSanPham.PageIndex = e.NewPageIndex;
            FillLoaiSanPham();
        }

        protected void GRLoaiSanPham_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GRLoaiSanPham.EditIndex = -1;
            FillLoaiSanPham();
        }

        protected void GRLoaiSanPham_RowUpdated(object sender, GridViewUpdatedEventArgs e)
        {
            GRLoaiSanPham.EditIndex = -1;
            FillLoaiSanPham();
        }

        protected void GRLoaiSanPham_RowEditing(object sender, GridViewEditEventArgs e)
        {
            // Chuyen sang che do Edit
            GRLoaiSanPham.EditIndex = e.NewEditIndex;
            FillLoaiSanPham();

        }

        protected void GRLoaiSanPham_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            
            if (e.NewValues == e.OldValues)
                e.Cancel = true;
            else
            {
                GridView gv = GRLoaiSanPham;
                TextBox txtTenLoai = (TextBox)gv.Rows[gv.EditIndex].Cells[1].Controls[0];
                if (gv.DataKeys != null)
                {
                    string maLoaiSanPham = gv.DataKeys[gv.EditIndex].Value.ToString();
                    LoaiSanPhamDTO loaiSanPhamDto = new LoaiSanPhamDTO();
                    loaiSanPhamDto.MaLoaiSanPham = maLoaiSanPham;
                    loaiSanPhamDto.TenLoaiSanPham = txtTenLoai.Text.Trim();
                    bool result = LoaiSanPhamBUS.UpdateLoaiSanPham(loaiSanPhamDto);
                    if (result)
                    {
                        gv.EditIndex = -1;
                        
                    }
                }
                GRLoaiSanPham.EditIndex = -1;
                FillLoaiSanPham();

            }
        }
       
        protected void GRLoaiSanPham_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            //  xóa quyen
            if (GRLoaiSanPham.DataKeys != null)
            {
                string maLoai = GRLoaiSanPham.DataKeys[e.RowIndex].Value.ToString();
                bool result = LoaiSanPhamBUS.DeleteLoaiSanPham(maLoai);
                if (result)
                {
                    GRLoaiSanPham.EditIndex = -1;
                    FillLoaiSanPham();
                }
            }
        }

        protected void GRLoaiSanPham_SelectedIndexChanged(object sender, EventArgs e)
        {
            string urlParams = string.Format("MaSanPham={0}", GRLoaiSanPham.SelectedDataKey.Values[0]);
            Response.Redirect("ThongTinSanPhamEditor.aspx?" + urlParams, true);		
        }

        protected void btnThem_Click(object sender, EventArgs e)
        {
            string maLoai = dataMaLoaiSanPham.Text.Trim();
            string tenLoai = dataTenLoaiSanPham.Text.Trim();
            if (!string.IsNullOrEmpty(maLoai) && !string.IsNullOrEmpty(tenLoai))
            {
                LoaiSanPhamDTO loaiSanPhamDto = new LoaiSanPhamDTO();
                loaiSanPhamDto.MaLoaiSanPham = maLoai;
                loaiSanPhamDto.TenLoaiSanPham = tenLoai;
                bool res = LoaiSanPhamBUS.InsertLoaiSanPham(loaiSanPhamDto);
                if (res)
                {
                    FillLoaiSanPham();
                }
            }
        }
    }
}